// pages/home/home.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: {//配置信息.
      "banner": {//swiper组件官方文档: https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html
        "indicatorDots": true,//是否显示面板指示点(默认: false , minSDKVersion: 1.0.0).
        "indicatorColor": "#F2F2F2",//指示点颜色(默认: rgba(0, 0, 0, .3) , minSDKVersion: 1.1.0).
        "indicatorActiveColor": "#228B22",//当前选中的指示点颜色(默认: #000000 , minSDKVersion: 1.1.0).
        "autoplay": true,//是否自动切换(默认: false , minSDKVersion: 1.0.0).
        "current": 0,//当前所在滑块的 index(默认: 0 , minSDKVersion: 1.0.0).
        "interval": 5000,//自动切换时间间隔(默认: 5000 , minSDKVersion: 1.0.0).
        "duration": 500,//滑动动画时长(默认: 500 , minSDKVersion: 1.0.0).
        "circular": true,//是否采用衔接滑动(默认: false , minSDKVersion: 1.0.0).
        "vertical": false,//滑动方向是否为纵向(默认: false , 设置为true没用? , minSDKVersion: 1.0.0).
        "previousMargin": "0px",//前边距，可用于露出前一项的一小部分，接受 px 和 rpx 值(默认: "0px" , minSDKVersion: 1.9.0).
        "nextMargin": "0px",//后边距，可用于露出后一项的一小部分，接受 px 和 rpx 值(默认: "0px" , minSDKVersion: 1.9.0).
        "displayMultipleItems": 1,//同时显示的滑块数量(默认:1 , 备注: 需小于size , minSDKVersion: 1.9.0).
        "skipHiddenItemLayout": false,//是否跳过未显示的滑块布局，设为 true 可优化复杂情况下的滑动性能，但会丢失隐藏状态滑块的布局信息(默认:false , minSDKVersion: 1.9.0).
        "easingFunction": "default",//指定 swiper 切换缓动动画类型(默认: "default" , 可选值: "default" - 默认缓动函数、"linear" - 线性动画、"easeInCubic" - 缓入动画、"easeOutCubic" - 缓出动画、"easeInOutCubic" - 缓入缓出动画 , minSDKVersion: 2.6.5).
        "image": {
          "mode": "scaleToFill",
        }
      },
    },
    banners: [],//轮播图数据.
    menus: [],//Menu数据.
    commodities: [],//商品列表数据.
    pageParams: {//商品列表分页参数.
      startPageNo: 0,
      pageNo: 0,
      pageSize: 10,
      isLastPage: false,
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("生命周期函数--onLoad--监听页面加载");

    //初始化Banner.
    this.initBanner();
    //初始化Menu.
    this.initMenu();
    //初始化商品列表数据.
    this.getCommoditiesData(this.data.pageParams.startPageNo, this.data.pageParams.pageSize);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("生命周期函数--onReady--监听页面初次渲染完成");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("生命周期函数--onShow--监听页面显示");

    wx.getSetting({
      success: res => {
        console.log("获取用户的当前设置：", res);
        if (!res.authSetting['scope.ususerLocationerInfo']) {
          console.log("小程序没有获取用户信息权限,准备申请权限：");
          wx.authorize({
            scope: 'scope.userLocation',
            success: function () {
              console.log("用户已同意小程序使用用户信息功能");
            }
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("生命周期函数--onHide--监听页面隐藏");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("生命周期函数--onUnload--监听页面卸载");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("页面相关事件处理函数--onPullDownRefresh--监听用户下拉动作");

    //商品列表数据—刷新.
    this.getCommoditiesData(this.data.pageParams.startPageNo, this.data.pageParams.pageSize);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("页面上拉触底事件的处理函数--onReachBottom");

    //商品列表数据——加载更多.
    if (!this.data.pageParams.isLastPage) {
      this.getCommoditiesData(++this.data.pageParams.pageNo, this.data.pageParams.pageSize);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log("用户点击右上角分享--onShareAppMessage");
  },

  /**
   * 初始化Banner.
   */
  async initBanner() {
    let banners = [];
    //"url": "http://a2.att.hudong.com/36/48/19300001357258133412489354717.jpg"
    for (let index = 0; index < 3; index++) {
      banners.push({
        "url": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      });
    }
    this.setData({
      banners: banners
    });
  },

  /**
   * 初始化Menu.
   */
  async initMenu() {
    let menus = [];

    menus = [
      {
        id: 1,
        "name": "蔬菜豆制品",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 2,
        "name": "肉蛋禽",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 3,
        "name": "鱼虾贝蟹",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 4,
        "name": "新鲜水果",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 5,
        "name": "乳品烘焙",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 6,
        "name": "粮油调味",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 7,
        "name": "方便速食",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 8,
        "name": "休闲零食",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 9,
        "name": "酒水饮料",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
      {
        id: 10,
        "name": "餐厨用具",
        "iconUrl": "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg"
      },
    ];

    this.setData({
      menus: menus
    });
  },

  /**
   * 分页获取商品列表数据.
   * @param {Number} pageNo 页码.
   * @param {Number} pageSize 分页大小.
   */
  async getCommoditiesData(pageNo, pageSize) {
    console.log("分页", pageNo, pageSize);
    wx.showLoading({
      title: '加载中',
      mask: true,
    });

    let commodities = pageNo != 0 ? this.data.commodities : [];

    for (let index = 0; index < pageSize; index++) {
      commodities.push({
        id: commodities.length,
        storeName: "中国店",
        storeLogoUrl: "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg",
        coverUrl: "http://n.sinaimg.cn/photo/1_img/upload/cf3881ab/799/w1000h599/20200527/c317-iufmpmn0437916.jpg",
        title: "洋鸡蛋 10枚 490g",
        price: 9.9,
      });
    }

    this.data.pageParams.pageNo = pageNo;
    this.setData({
      "pageParams.isLastPage": commodities.length >= 30 ? true : false,
      commodities: commodities,
    });

    setTimeout(() => {
      wx.hideLoading({
        complete: (res) => {
          console.log("关闭loading成功");
        },
      })
    }, 500);
  },

  /**
   * Menu item点击监听事件.
   * @param {Event} event 事件对象.
   */
  tapMenuItem: function (event) {
    let menuData = this.data.menus[event.currentTarget.dataset.index];
    console.log("menu=", menuData);

    //由于switchTab不支持传递参数,故在这里将将要选择的分类ID设置为全局变量,在分类页面再从全局变量里获取数据.
    console.log("全局数据", app);
    app.globalData.category.preSelectedCategoryId = menuData.id;

    wx.switchTab({
      url: '../category/category?categoryId={{menuData.id}}',
    })
  },

})